package com.sec.etech.employeesync.schedule;

import com.sec.etech.employeesync.entity.OrgEmployeeZCompany;
import com.sec.etech.employeesync.mapper.OrgEmployeeZCompanyMapper;
import com.sec.etech.ism.controller.IsmController;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 齐马的作品
 * @version : 1.0
 * @date : 2024/8/30 下午12:47
 * @description:
 */
@Component
@Slf4j
public class SyncSapEmployeeScheduled {

    @Resource
    private OrgEmployeeZCompanyMapper companyMapper;
    @Autowired
    private IsmController ismController;

    // 每周日上午8点同步一次
    @Scheduled(cron = "0 0 8 ? * SUN")
//    @Scheduled(cron = "30 4 13 ? * FRI")
    public void syncSapEmployee() {
//        IUser currentUser = ContextUtil.getCurrentUser();
//        if (currentUser == null) {
//            ContextUtil.setCurrentUser(EtechUserUtil.getUserAdmin());
//        }

        syncSapEmployee("417137619324108800");
    }

    public ResultMsg syncSapEmployee(String currentUserId) {
        List<OrgEmployeeZCompany> companyList = companyMapper.selectList(null);

        String code = companyList.parallelStream()
                .filter(company -> {
                    Boolean enable = company.getEnable();
                    return enable != null && enable;
                })
                .map(OrgEmployeeZCompany::getCode)
                .collect(Collectors.joining(","));

//        log.info("同步SAP员工数据，公司代码：{}", code);

        return ismController.employeesync("company", code, currentUserId, null);
    }
}
